package org.achievement.system.config;

import org.achievement.system.common.RequestResult;
import org.achievement.system.common.exception.AuthenticationException;
import org.achievement.system.common.exception.ServiceException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = ServiceException.class)
    @ResponseBody
    public RequestResult exceptionHandler(HttpServletRequest httpServletRequest, ServiceException e){
        return RequestResult.error(e.getMessage());
    }
    @ExceptionHandler(value = AuthenticationException.class)
    @ResponseBody
    public RequestResult authenticationExceptionHandler(HttpServletRequest httpServletRequest, ServiceException e){
        return RequestResult.error(401, e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public RequestResult exceptionHandler(HttpServletRequest httpServletRequest, Exception e){
        e.printStackTrace();
        return RequestResult.error(e.getMessage());
    }

}
